package localhost.mb;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.faces.model.SelectItem;

public class MetodosComuns {

	Locale ptBR = new Locale("pt", "BR");

	public String converteStringVrBrasilParaAmericano(String valor) {
		String mf = valor.replace(".", "");
		mf = mf.replace(",", ".");
		mf = mf.replace("R$", "");
		return mf.trim();

	}

	public String converteStringVrAmericanoParaBrasil(String valor) {
		String mf = valor.replace(",", "");
		mf = mf.replace(".", ",");
		return mf.trim();

	}

	public String formatoMoedaReal(BigDecimal b) {
		NumberFormat moedaFormat = NumberFormat.getCurrencyInstance(ptBR);
		return moedaFormat.format(b);
	}

	public int retornaAnoReferentePeriodo(Date agora) {
		int anoref0 = 0, anoref1 = 0, anoref2 = 0;
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(agora);
		cal2.set(cal1.get(Calendar.YEAR), Calendar.MARCH, 1);
		anoref1 = cal1.get(Calendar.YEAR);
		anoref2 = cal2.get(Calendar.YEAR);
		if (cal1.before(cal2)) {
			anoref0 = anoref1 - 1;
		} else {
			anoref0 = anoref2;
		}
		return anoref0;
	}

	public int retornaAnoReferente(Date agora) {
		int anoref = 0;
		Calendar cal = Calendar.getInstance();
		cal.setTime(agora);
		anoref = cal.get(Calendar.YEAR);
		return anoref;
	}

	public boolean isNumeric(String str) {
		for (char c : str.toCharArray()) {
			if (!Character.isDigit(c))
				return false;
		}
		return true;
	}

	public List<SelectItem> getComboTipo() {

		List<SelectItem> selectI = new ArrayList<SelectItem>();
		selectI.add(new SelectItem("E", "Entrada"));
		selectI.add(new SelectItem("S", "Saída"));
		selectI.add(new SelectItem("A", "Ambos"));
		return selectI;

	}

	public String capitalizandoPrimeiraLetra(String s) {
		if (s.length() == 0)
			return s;
		return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
	}

	public String capitalizandoCadaPalavra(String string) {
		char[] chars = string.toLowerCase().toCharArray();
		boolean found = false;
		for (int i = 0; i < chars.length; i++) {
			if (!found && Character.isLetter(chars[i])) {
				chars[i] = Character.toUpperCase(chars[i]);
				found = true;
			} else if (Character.isWhitespace(chars[i]) || chars[i] == '.'
					|| chars[i] == '\'' || chars[i] == '-' || chars[i] == '&') { // You
																					// can
																					// add
																					// other
																					// chars
																					// here
				found = false;
			}
		}
		return String.valueOf(chars);
	}

}